package com.example.bookshelf.data;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.example.bookshelf.R;

import java.io.Serializable;

public class BookItem implements Serializable, Parcelable {
    private int imageID;               // 图片
    private String title;              // 标题
    private String author;             // 作者
    private String translator;         // 译者
    private String press;              // 出版社
    private int publishedYear;         // 出版年份    保证有年，不一定有月份
    private int publishedMonth;        // 出版月份    保证有年，不一定有月份
    private String ISBN;               // ISBN

    private String readingStatus;         // 阅读状态
    private String bookshelfName;           // 书架号
    private String notes;              // 笔记
    private String labels;              // 标签
    private String url;                // 网络

    public BookItem() {
        imageID = R.drawable.ic_main_book_default;
        notes = "";
        labels = "";
        title = "";
        author = "";
        translator = "";
        press = "";
        bookshelfName = "";
        readingStatus = "未设置";
    }

    protected BookItem(Parcel in) {
        imageID = in.readInt();
        title = in.readString();
        author = in.readString();
        translator = in.readString();
        press = in.readString();
        publishedYear = in.readInt();
        publishedMonth = in.readInt();
        ISBN = in.readString();
        readingStatus = in.readString();
        bookshelfName = in.readString();
        notes = in.readString();
        labels = in.readString();
        url = in.readString();
    }

    public static final Creator<BookItem> CREATOR = new Creator<BookItem>() {
        @Override
        public BookItem createFromParcel(Parcel in) {
            return new BookItem(in);
        }

        @Override
        public BookItem[] newArray(int size) {
            return new BookItem[size];
        }
    };

    // 该函数返回textViewInfo中的显示内容
    public String getInfo() {
        String info = "";
        if (null != this.author) {
            info += this.author;
        }
        if (null != this.press) {
            info += " ";
            info += this.press;
        }
        return  info;
    }

    // 未完成 !!!!!!
    // 该函数返回textViewDate中的显示内容，
    public String getDate() {
        String date = "";
        if (0 != this.publishedYear) {
            date += this.publishedYear;
            if (0 != this.publishedMonth) {
                date += "-" + this.publishedMonth;
            }
        }
        return  date;
    }


    // 根据bundle中的数据创建类
    static public BookItem getBookItemFromBundle(Bundle bundle) {
        BookItem bookItem = new BookItem();
        bookItem.setTitle(bundle.getString("title", ""));
        bookItem.setAuthor(bundle.getString("author", ""));
        bookItem.setTranslator(bundle.getString("translator", ""));
        bookItem.setPress(bundle.getString("press", ""));

        Log.d("test", bundle.getString("publishedYear"));
        Log.d("test", bundle.getString("press"));

        String publishedYear = bundle.getString("publishedYear");
        if (publishedYear.equals(""))
            bookItem.setPublishedYear(0);
        else
            bookItem.setPublishedYear(Integer.parseInt(publishedYear));

        String publishedMonth = bundle.getString("publishedMonth");
        if (publishedMonth.equals(""))
            bookItem.setPublishedMonth(0);
        else bookItem.setPublishedMonth(Integer.parseInt(publishedMonth));

        bookItem.setISBN(bundle.getString("ISBN", ""));

        bookItem.setNotes(bundle.getString("notes"));
        bookItem.setLabels(bundle.getString("label", ""));
        bookItem.setUrl(bundle.getString("url",""));

        return bookItem;
    }

// -------------------------getter and setter-------------------------------------------------------
    public int getImageID() {
        return imageID;
    }

    public void setImageID(int imageID) {
        this.imageID = imageID;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTranslator() {
        return translator;
    }

    public void setTranslator(String translator) {
        this.translator = translator;
    }

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public int getPublishedYear() {
        return publishedYear;
    }

    public void setPublishedYear(int publishedYear) {
        this.publishedYear = publishedYear;
    }

    public int getPublishedMonth() {
        return publishedMonth;
    }

    public void setPublishedMonth(int publishedMonth) {
        this.publishedMonth = publishedMonth;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public String getReadingStatus() {
        return readingStatus;
    }

    public void setReadingStatus(String readingStatus) {
        this.readingStatus = readingStatus;
    }

    public String getBookshelfName() {
        return bookshelfName;
    }

    public void setBookshelfName(String bookshelfName) {
        this.bookshelfName = bookshelfName;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getLabels() {
        return labels;
    }

    public void setLabels(String labels) {
        this.labels = labels;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


    public void upData(BookItem bookItem) {
        imageID = bookItem.getImageID();
        title = bookItem.getTitle();
        author = bookItem.getAuthor();
        translator = bookItem.getTranslator();
        press = bookItem.getPress();
        publishedYear = bookItem.getPublishedYear();
        publishedMonth = bookItem.publishedMonth;
        ISBN = bookItem.getISBN();

        readingStatus = bookItem.getReadingStatus();
        bookshelfName = bookItem.getBookshelfName();
        notes = bookItem.getNotes();
        labels = bookItem.getLabels();
        url = bookItem.getUrl();
    }



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(imageID);
        parcel.writeString(title);
        parcel.writeString(author);
        parcel.writeString(translator);
        parcel.writeString(press);
        parcel.writeInt(publishedYear);
        parcel.writeInt(publishedMonth);
        parcel.writeString(ISBN);
        parcel.writeString(readingStatus);
        parcel.writeString(bookshelfName);
        parcel.writeString(notes);
        parcel.writeString(labels);
        parcel.writeString(url);
    }
}
